// pages/mine/address/address.js
import Tips from '../../class/utils/Tips.js'
//获取应用实例
var app = getApp()
Page({
  data: {
   
    allAddress: [], //地址列表
  },
  // --------------------------------------------------------
  // 垃圾桶删除当前指定地址
  deleteAddr: function(e) {
    console.log(e)

    let that = this;
    let uid=that.data.uid;
    let allAddress = that.data.allAddress;
    let index = e.currentTarget.dataset.index;
    let user_id = allAddress[index].id;
    console.log(allAddress, that.data.uid, user_id)
    allAddress.splice(index, 1);
    this.setData({
      allAddress: allAddress
    });
    // 获取到要删除地址的id
    // 删除请求-------------------------
    Tips.loading()
    wx.request({
      url: app.globalData.api + 'delAddress',
      data: {
        uid: that.data.uid,
        id: user_id
      },
      success: function(res) {
        Tips.loaded();
        let result = res.data.code;
        if (res.data.code==0){
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          })
        }
       
      
  

      }
    });
    //把数据回填
  },
  // 编辑地址
  updateAddr: function(e) {
    console.log(e)
    let that=this
    var allAddress = that.data.allAddress;
    console.log(allAddress)
    var index = e.currentTarget.dataset.index;

    wx.navigateTo({
      url: "/pages/address/addup/addup?take=" + JSON.stringify(allAddress[index])
    })
  },
  // 选为默认
  selectMoren: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var user = wx.getStorageSync('user');
    var pid = user
    var allAddress = this.data.allAddress;
    var id = allAddress[index].id;
    console.log(pid);

    for (var i = 0; i < allAddress.length; i++) {
      allAddress[i].status = '';
    }
    allAddress[index].status = '1';
    that.setData({
      allAddress: allAddress
    });
    // 默认控制接口
    Tips.loading();
    wx.request({
      url: app.globalData.hpUrl + 'set_address_default',
      data: {
        id: id,
      },
      success: function(res) {
        var result = res.data;
        console.log(res);
        // wx.showToast({
        //   title: '成功默认',
        //   icon: 'success',
        //   duration: 1000
        // })
        Tips.loaded();
        Tips.toast('成功默认')
        that.onShow()
      }
    });
  },

  onLoad: function() {
    // let that = this;

    // wx.getStorage({
    //   key: 'userinfo',
    //   success: function (res) {
    //     console.log('缓存', res.data)
    //   }
    // })
   
  },
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log(res.data.id)
        let uid = res.data.id
        that.setData({
          uid: uid
         
        })
        wx.request({
          url: app.globalData.api + 'getAddress',   //我的收货地址列表
          data: {
            uid: that.data.uid,
            page: 1,
            limit: 10
          },
          success: function (res) {
            console.log(res)
            var result = res.data.list
            console.log(result);
            that.setData({
              allAddress:result
            })
            // if (result == user.id) {

            // } else {
            //   that.setData({
            //     allAddress: result
            //   });
            // }

          }
        });
      },
    })
  },

  addrss: function() {
    wx.navigateTo({
      url: '/pages/address/addinto/addinto',
    })
  }
})